//
//  UIViewController+SMCategory.m
//  DiDi
//
//  Created by huangtie on 2017/3/18.
//  Copyright © 2017年 Sinmore. All rights reserved.
//

#import "UIViewController+SMCategory.h"

@implementation UIViewController (SMCategory)

+ (UIViewController *)visibleController {
    @try {
        UIViewController *focusViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
        UITabBarController *tbc = (UITabBarController *)focusViewController;
        if([tbc respondsToSelector:@selector(selectedViewController)]) {
            NSArray* arrayViewControllers = [tbc.selectedViewController childViewControllers];
            if(arrayViewControllers != nil) {
                return arrayViewControllers.lastObject;
            }
        }
        return tbc;
    } @catch(NSException *exception) { } @finally { }
}

- (void)removeFromNavControllers
{
    NSMutableArray <UIViewController *>*array = [NSMutableArray array];
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if (controller != self) {
            [array addObject:controller];
        }
    }
    if (array.count) {
        array.lastObject.navigationController.viewControllers = array;
    }
}

@end
